package com.appspile.android.ANMP.applocksexy.application.service;

import android.app.ActivityManager;
import android.app.IntentService;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

import com.appspile.android.ANMP.applocksexy.application.activity.LockScreenActivity;

import java.util.List;

public class LockService extends IntentService {

    private static ActivityManager activityManager;
    private static String TAG = "Activity Running Application";

    public LockService() {

        super("CheckRunningApplications");


    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        return START_STICKY;
    }

    public static void startService(Context context) {
        Intent intent = new Intent(context, LockService.class);
        context.startService(intent);

        if (activityManager == null) {
            activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        }
    }

    @Override
    public void onCreate() {
        super.onCreate();
    }

    @Override
    public void onStart(Intent intent, int startId) {
        super.onStart(intent, startId);
    }

    public static ActivityManager.RunningTaskInfo getTopActivity(ActivityManager activityManager) {
        ActivityManager.RunningTaskInfo runningTaskInfo = null;
        List<ActivityManager.RunningTaskInfo> appTaskList = activityManager.getRunningTasks(1);
        runningTaskInfo = appTaskList.get(0);

        return runningTaskInfo;
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        Log.d(TAG, "Receive Intent : " + intent);
        if (intent != null) {
            Log.d(TAG, "Package name check package");
            handleActionCheckRunningApplication("", "");
        }
    }

    private void handleActionCheckRunningApplication(String param1, String param2) {
        // TODO: Handle action Baz
        //throw new UnsupportedOperationException("Not yet implemented");

        ActivityManager.RunningTaskInfo taskRunningInfo = getTopActivity(activityManager);
        String packageName = taskRunningInfo.topActivity.getPackageName();
        if (packageName != null) {
            Log.d(TAG, "============= Launcher Lock Screen ===================");
            Log.d(TAG, packageName);
           // ToastUtils.showLong(GlobalApp.getInstance(), packageName);

            Intent intent;
            intent = new Intent(this, LockScreenActivity.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(intent);



        }
    }
}
